// pages/confirm/confirm.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    display: "none",
    _num: 1,
    order_no: '',
    coupon: '优惠券',
    array: [],
    useid: -1,
    distribution_pay: 1, //0-->自提 1-->配送,
  },
  //优惠券
  bindPickerChange: function (e) {
    let that = this;
    var money = that.data.money;    //价格    Total
    var Total = that.data.Total;     //合计价格
    //使用优惠券
    if (that.data.array[e.detail.value].useid != -1) {
      if (money >= that.data.array[e.detail.value].full_money) {
        console.log('使用优惠券')
        console.log(money - that.data.array[e.detail.value].money)
        Total = money - that.data.array[e.detail.value].money
        that.setData({
          index: e.detail.value,
          coupon: that.data.array[e.detail.value].name,
          useid: that.data.array[e.detail.value].useid,
          Total: Total
        })
      } else {      //优惠券未达到额度
        wx.showToast({
          title: '优惠券未达到使用额度',
          icon: 'none'
        })
        Total = money
        that.setData({
          coupon: '未使用优惠券',
          useid: -1,
          Total: money
        })
      }
    } else {
      that.setData({
        coupon: '未使用优惠券',
        useid: -1,
        Total: money
      })
      console.log('未用优惠券')
    }

    // console.log(Total)
    // console.log('picker发送选择改变，携带值为', e.detail.value)


  },
  liuyan: function (e) {
    this.setData({
      liuyan: e.detail.value
    })
  },
  show_payment: function () {
    let that = this;
    wx.request({
      url: app.data.Url + 'order/insertOrder',
      data: {
          cart_data:{
            cartid: that.data.cartid,
            order_remark: that.data.liuyan,
          },
        uid: wx.getStorageSync('userInfo').uid,
        distribution_pay: that.data.distribution_pay
      },
      method: 'post',
      success: function (res) {
        console.log('---------------', res.data.data.order_no)
        that.setData({
          order_no: res.data.data.order_no
        })
      }
    })
    this.setData({
      display: "block"
    })
  },
  hideview: function () {
    let that = this;
    console.log(that.data.order_no, wx.getStorageSync('userInfo').uid)
    wx.request({
      url: app.data.Url + 'order/orderPayParams',
      data: {
        order_no: that.data.order_no,
        title: '智慧社区订单支付',
        uid: wx.getStorageSync('userInfo').uid
      },
      method: 'post',
      success: function (res) {
        wx.requestPayment({
          'timeStamp': res.data.data.weixin.timeStamp,
          'nonceStr': res.data.data.weixin.nonceStr,
          'package': res.data.data.weixin.package,
          'signType': 'MD5',
          'paySign': res.data.data.weixin.paySign,
          'success': function (res) {

          },
          'fail': function (res) {
            wx.navigateTo({
              url: '../order/order?num=1',
            })
          }
        })
      }
    })
    this.setData({
      display: "none"
    })
  },
  focus_click: function (e) {

    console.log(e.target.dataset.num);
    if (e.target.dataset.num == 2) {
      //自提
      this.setData({
        distribution_pay: 0
      })
    } else {
      this.setData({
        distribution_pay: 1
      })
    }

    this.setData({
      _num: e.target.dataset.num
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
      money:options.money,
      cartid:options.cartid
    })
    wx.request({
      url: app.data.Url + 'cart/get_detail',
      data: {
        cartid: options.cartid
      },
      method:'post',
      success: function (res) {
        that.setData({
          detail: res.data.data,
          num: options.num,
          money:options.money,
          Total: options.money,
          goodsid: options.goodsid
        })
        console.log(res.data.data)
      }
    })
    //优惠券
    //https://zhapp.4hl.cn/app/Coupon/usercoupon?uid=XXX&youtype=XXX
    wx.request({
      url: 'https://zhapp.4hl.cn/app/Coupon/usercoupon',
      data: {
        uid: wx.getStorageSync('userInfo').uid,
        youtype: 1
      },
      method: 'get',
      success: function (res) {
        console.log(res.data)
        that.setData({
          array: res.data.data
        })

      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    wx.getStorage({
      key: 'address',
      success: function (res) {
        console.log(res)
        that.setData({
          add: res.data
        })
        wx.removeStorage({
          key: 'address',
          success: function (res) {
            console.log(res.data)
          }
        })
      },
      fail:function(){
        wx.request({
          url: app.data.Url + 'address/get_defaultadd',
          data: {
            uid: wx.getStorageSync('userInfo').uid,
          },
          method: 'get',
          success: function (res) {
            if (!res.data.data.city) {
              wx.showModal({
                title: '提示',
                content: '请添加默认地址',
                showCancel: false,
                success: function () {
                  wx.navigateTo({
                    url: '/pages/address/address',
                  })
                }
              })
            } else {
              that.setData({
                add: res.data.data
              })
            }
          }
        })
      }
    })

    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})